package task6

import org.apache.spark.sql.SparkSession

object SQLPractice {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .master("local[*]")
      .appName(s"${this.getClass.getCanonicalName}")
      .getOrCreate()

    import spark.implicits._
    val ds = spark.sparkContext.makeRDD(Seq(Data("1", "2019-03-04", "2020-02-03"), Data("2", "2020-04-05", "2020-08-04"), Data("3", "2019-10-09", "2020-06-11"))).toDS

    ds.createOrReplaceTempView("org")
    spark.sql(
      """
        |with tmp as (
        |select startdate as start from org
        |union
        |select enddate as start from org
        |)
        |select start, nvl(lead(start) over(order by start), start) as end from tmp
        |""".stripMargin)
      .show()

    spark.close()
  }
  case class Data (id: String, startdate: String, enddate:String)
}
